import bb.cascades 1.3
import "lib"
import "controls"

Page {
    id: root
    property bool isReg: _Setting.getReg("0") == 1

    titleBar: TitleBar {
        title: qsTr("Setting")
    }
    
    function openRegist(){
        var docCom = Qt.createComponent("asset:///Register.qml")
        var docView = docCom.createObject(navRoot);
        docView.registed.connect(onRegisted);
        navRoot.push(docView);
    }
    
    function openCache(){
        var docCom = Qt.createComponent("asset:///Cache.qml")
        var docView = docCom.createObject(navRoot);
        navRoot.push(docView);
    }
    
    function onRegisted(){
        creationCompleted();
    }

    function setStyle(isdark) {
        console.log(isdark)
        //Dark:true   Bright:false 保存设置
        _Setting.setStyle(isdark ? "Dark" : "Bright")

        //设置样式
        var style = _Setting.getStyle("Bright");
        Application.themeSupport.setVisualStyle(style == "Dark" ? VisualStyle.Dark : VisualStyle.Bright);

        creationCompleted()
    }

    function setImagesMode(mode) {
        _Setting.setMode(mode ? "NoImages" : "Images")
    }

    function setPushInfo(pushinfo) {
        _Setting.setPushInfo(pushinfo ? "PushInfo" : "NoPushInfo")
    }

    onCreationCompleted: {
        theDataModel.clear();
        isReg = _Setting.getReg("0") == 1;
        var result = []
        result.push({
                "title": qsTr("Dark"),
                "itemType": "Dark",
                "screenWidth": style.screenWidth,
                "itemHeight": 100,
                "root": root,
                "style": style,
                "value": _Setting.getStyle("Bright"),
                "index": 1,
                "isReg": true
            }, {
                "title": qsTr("No image(s) mode"),
                "itemType": "NoImages",
                "screenWidth": style.screenWidth,
                "itemHeight": 100,
                "root": root,
                "style": style,
                "value": _Setting.getMode("Images"),
                "index": 2,
                "isReg": isReg
            }, {
                "title": qsTr("Push information"),
                "itemType": "PushInfo",
                "screenWidth": style.screenWidth,
                "itemHeight": 100,
                "root": root,
                "style": style,
                "value": _Setting.getPushInfo("PushInfo"),
                "index": 3,
                "isReg": isReg
            }, {
                "title": qsTr("Cache Manager"),
                "itemType": "Cache",
                "screenWidth": style.screenWidth,
                "itemHeight": 100,
                "root": root,
                "style": style,
                "index": 4,
                "isReg": isReg
            });

        result.push({
                "title": qsTr("Regist Status"),
                "itemType": "Regist",
                "screenWidth": style.screenWidth,
                "itemHeight": 100,
                "root": root,
                "style": style,
                "value": _Setting.getReg("0"),
                "index": 5,
                "isReg": isReg
            });

        theDataModel.insertList(result)
    }

    Container {
        ListView {
            id: view

            dataModel: GroupDataModel {
                id: theDataModel
                grouping: ItemGrouping.None
                sortedAscending: true
                sortingKeys: [ "index" ]
            }

            function itemType(data, indexPath) {
                return data.itemType;
            }

            onTriggered: {
            }

            listItemComponents: [
                ListItemComponent {
                    type: "Dark"
                    MenuItem {
                        Padding {
                            preferredWidth: ListItemData.screenWidth
                            preferredHeight: ListItemData.itemHeight
                            layout: DockLayout {
                            }

                            Container {
                                horizontalAlignment: HorizontalAlignment.Left
                                verticalAlignment: VerticalAlignment.Center
                                Label {
                                    text: qsTr("%1%2").arg(ListItemData.title).arg(ListItemData.isReg?"":"(需注册)")
                                    textStyle.base: ListItemData.style.titleStyle
                                }
                            }

                            Container {
                                horizontalAlignment: HorizontalAlignment.Right
                                verticalAlignment: VerticalAlignment.Center
                                ToggleButton {
                                    id: dark
                                    enabled: ListItemData.isReg
                                    checked: ListItemData.value == "Bright" ? false : true
                                    onCheckedChanged: {
                                        var root = ListItemData.root
                                        root.setStyle(checked)
                                    }
                                }
                            }
                        }

                        Divider {
                            topMargin: 0
                            bottomMargin: 0
                        }
                    }
                },

                ListItemComponent {
                    type: "NoImages"
                    MenuItem {
                        Padding {
                            preferredWidth: ListItemData.screenWidth
                            preferredHeight: ListItemData.itemHeight
                            layout: DockLayout {
                            }

                            Container {
                                horizontalAlignment: HorizontalAlignment.Left
                                verticalAlignment: VerticalAlignment.Center
                                Label {
                                    text: qsTr("%1%2").arg(ListItemData.title).arg(ListItemData.isReg?"":"(需注册)")
                                    textStyle.base: ListItemData.style.titleStyle
                                }
                            }

                            Container {
                                horizontalAlignment: HorizontalAlignment.Right
                                verticalAlignment: VerticalAlignment.Center
                                ToggleButton {
                                    id: noImages
                                    enabled: ListItemData.isReg
                                    checked: ListItemData.value == "Images" ? false : true
                                    onCheckedChanged: {
                                        var root = ListItemData.root
                                        root.setImagesMode(checked)
                                    }
                                }
                            }
                        }

                        Divider {
                            topMargin: 0
                            bottomMargin: 0
                        }
                    }
                },

                ListItemComponent {
                    type: "PushInfo"
                    MenuItem {
                        Padding {
                            preferredWidth: ListItemData.screenWidth
                            preferredHeight: ListItemData.itemHeight
                            layout: DockLayout {
                            }

                            Container {
                                horizontalAlignment: HorizontalAlignment.Left
                                verticalAlignment: VerticalAlignment.Center
                                Label {
                                    text: qsTr("%1%2").arg(ListItemData.title).arg(ListItemData.isReg?"":"(需注册)")
                                    textStyle.base: ListItemData.style.titleStyle
                                }
                            }

                            Container {
                                horizontalAlignment: HorizontalAlignment.Right
                                verticalAlignment: VerticalAlignment.Center
                                ToggleButton {
                                    id: noPushInfo
                                    enabled: ListItemData.isReg
                                    checked: ListItemData.value == "PushInfo" ? true : false
                                    onCheckedChanged: {
                                        var root = ListItemData.root
                                        root.setPushInfo(checked)
                                    }
                                }
                            }
                        }

                        Divider {
                            topMargin: 0
                            bottomMargin: 0
                        }
                    }
                },
                
                //-------------------注冊
                ListItemComponent {
                    type: "Regist"
                    MenuItem {
                        Padding {
                            preferredWidth: ListItemData.screenWidth
                            preferredHeight: ListItemData.itemHeight
                            layout: DockLayout {
                            }
                            
                            Container {
                                horizontalAlignment: HorizontalAlignment.Left
                                verticalAlignment: VerticalAlignment.Center
                                Label {
                                    text: qsTr("%1%2").arg(ListItemData.title).arg(ListItemData.isReg?"":"(未注册)")
                                    textStyle.base: ListItemData.style.titleStyle
                                }
                            }
                            
                            Container {
                                horizontalAlignment: HorizontalAlignment.Right
                                verticalAlignment: VerticalAlignment.Center
                                
                                Container {
                                    //已注冊
                                    visible: ListItemData.value == 1
                                    background: Color.Red
                                    Label {
                                        text: qsTr("Registed")
                                        textStyle.base: SystemDefaults.TextStyles.SubtitleText
                                        textStyle.color: Color.White
                                        textStyle.fontStyle: FontStyle.Italic
                                    }
                                }
                                
                                Container {
                                    //未注冊
                                    visible: ListItemData.value == 0
                                    verticalAlignment: VerticalAlignment.Center
                                    horizontalAlignment: HorizontalAlignment.Center                            
                                    Button {
                                        text: qsTr("Regist")
                                        preferredWidth: 100
                                        onClicked: {
                                            var root = ListItemData.root
                                            root.openRegist();
                                        }
                                    }
                                }
                            }
                        }
                        
                        Divider {
                            topMargin: 0
                            bottomMargin: 0
                        }
                    }
                },
                
                
                ListItemComponent {
                    type: "Cache"
                    MenuItem{
                        Padding {
                            preferredWidth: ListItemData.screenWidth
                            preferredHeight: ListItemData.itemHeight
                            layout: DockLayout {
                            }
                            
                            Container {
                                horizontalAlignment: HorizontalAlignment.Left
                                verticalAlignment: VerticalAlignment.Center
                                Label {
                                    text: qsTr("%1%2").arg(ListItemData.title).arg(ListItemData.isReg?"":"(需注册)")
                                    textStyle.base: ListItemData.style.titleStyle
                                }
                            }
                        }
                        
                        onTouch: {
                            if(event.isDown()){
                                var root = ListItemData.root;
                                if(ListItemData.isReg){
                                    root.openCache();
                                }else{
                                    root.openRegist();
                                }
                                
                            }
                        }
                        
                        Divider {
                            topMargin: 0
                            bottomMargin: 0
                        }
                    }
                }
                
            ]
        }

        attachedObjects: [
            Common {
                id: style
            }
        ]
    }

    actions: [
        ActionItem {
            title: qsTr("Clear regist.")
            
            ActionBar.placement: ActionBarPlacement.Signature

            onTriggered: {
                _Setting.setReg("0")
            }
        },
        ActionItem {
            title: qsTr("Restart.")
            ActionBar.placement: ActionBarPlacement.InOverflow
            
            onTriggered: {
                app.restartService();
            }
        }
    ]
}
